package com.yangwei.examination.teacher.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.student.pojo.AnswerPaper;
import com.yangwei.examination.student.pojo.AnswerPaperChoice;
import com.yangwei.examination.student.pojo.AnswerPaperQuestion;
import com.yangwei.examination.student.service.AnswerPaperChoiceService;
import com.yangwei.examination.student.service.AnswerPaperQuestionService;
import com.yangwei.examination.student.service.AnswerPaperService;
import com.yangwei.examination.teacher.service.*;
import com.yangwei.examination.user.service.TeacherService;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Api(tags = "老师操作答卷",description = "老师操作答卷")
@RestController
@RequestMapping("/tea/AnswerPaper")
public class AnswerPaperController {

    @Resource
    private PaperService paperService;

    @Resource
    private PaperQuestionService paperQuestionService;

    @Resource
    private PaperQuestionChoiceService paperQuestionChoiceService;

    @Resource
    private PaperQuestionAnswerService paperQuestionAnswerService;

    @Resource
    private AnswerPaperService answerPaperService;

    @Resource
    private AnswerPaperQuestionService answerPaperQuestionService;

    @Resource
    private AnswerPaperChoiceService answerPaperChoiceService;

    @Resource
    private ExamService examService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private CorrectAnswerPaperService correctAnswerPaperService;

    @Resource
    private CorrectAnswerPaperScoreService correctAnswerPaperScoreService;

    @Resource
    private CorrectAnswerPaperInfoService correctAnswerPaperInfoService;


    @ApiOperation(value = "打回(清除)学生的答卷信息")
    //打回答卷 即删除学生的答卷
    @GetMapping("/sendBack")
    @Transactional
    public ResultVo sendBackForRedo(@RequestParam("examId") Long examId, @RequestParam("studentId") Long studentId) {

        System.out.println("----examId:" + examId + "----studentId:" + studentId);

        AnswerPaper one = answerPaperService.getOne(Wrappers.lambdaQuery(AnswerPaper.class)
                .eq(AnswerPaper::getExamId, examId)
                .eq(AnswerPaper::getStudentId, studentId)
        );

        if (one != null) {

            List<AnswerPaperQuestion> answerPaperQuestionList = answerPaperQuestionService.list(Wrappers.lambdaQuery(AnswerPaperQuestion.class)
                    .eq(AnswerPaperQuestion::getAnswerPaperId, one.getId())
            );

            //删除答卷选项
            if (answerPaperQuestionList.size()!=0){
                for (AnswerPaperQuestion answerPaperQuestion:answerPaperQuestionList){
                    answerPaperChoiceService.remove(Wrappers.lambdaQuery(AnswerPaperChoice.class)
                            .eq(AnswerPaperChoice::getAnswerPaperQuestionId,answerPaperQuestion.getId())
                    );
                }

                //删除答卷问题
                List<Long> ids = answerPaperQuestionList.stream()
                        .map(answerPaperQuestion -> answerPaperQuestion.getId())
                        .collect(Collectors.toList());
                answerPaperQuestionService.removeByIds(ids);
            }

            //删除答卷
            answerPaperService.removeById(one.getId());

        }


        return ResultUtils.success("打回成功！");
    }


}
